import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'

Vue.use(Router)

/**
 *  所有不需要权限的页面
 * */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '看板', icon: 'dashboard' }
    }]
  },
  {
    path: '/order',
    component: Layout,
    redirect: '/order/unsettled',
    name: 'order',
    meta: {
      title: '账单目录',
      icon: 'nested'
    },
    children: [
      {
        path: 'unsettled',
        component: () => import('@/views/order/index'),
        name: 'unsettled',
        meta: { title: '未结算订单' }
      },
      {
        path: 'settling',
        component: () => import('@/views/order/index'),
        name: 'settling',
        meta: { title: '结算中订单' }
      },
      {
        path: 'settled',
        component: () => import('@/views/order/index'),
        name: 'settled',
        meta: { title: '已结算订单' }
      },
      {
        path: 'cancel',
        component: () => import('@/views/order/index'),
        name: 'cancel',
        meta: { title: '已取消订单' }
      },
      {
        path: 'refund',
        component: () => import('@/views/order/index'),
        name: 'refund',
        meta: { title: '已退款订单' }
      }
    ]
  }
  // {
  //   path: '/service',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/service/index'),
  //       name: 'Icons',
  //       meta: { title: '服务目录', icon: 'icon', noCache: true, roles: ['sale'] }
  //     }
  //   ]
  // },
]

/**
 *
 * @type {*[]}
 */
export const asyncRoutes = [
  {
    path: '/money',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/service/index'),
        name: 'table',
        meta: { title: '财务清单', icon: 'eye', noCache: true, roles: ['sale'] }
      }
    ]
  },
  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://baidu.com',
        meta: { title: '客户管理', icon: 'link' }
      }
    ]
  },
  {
    path: '/statement',
    component: Layout,
    meta: {
      title: '结算单 admin',
      icon: 'order',
      roles: ['admin'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/statement/page'),
        name: 'PagePermission',
        meta: {
          title: 'admin 权限',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/statement/directive'),
        name: 'DirectivePermission',
        meta: {
          title: '不需要权限'
        }
      },
      {
        path: 'role',
        component: () => import('@/views/statement/role'),
        name: 'RolePermission',
        meta: {
          title: 'Role admin',
          roles: ['admin']
        }
      }
    ]
  },
  {
    path: '/service',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/service/index'),
        name: 'table',
        meta: { title: '服务目录', icon: 'eye', noCache: true, roles: ['sale'] }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
